Hệ thống quản lý bệnh viện bằng C# với mã nguồn

1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Threading.Tasks;
9 using
System.Windows.Forms;
10 using
System.Data.SqlClient;
11
12 namespace
HospitalManagementSystemCSharp
13 {
14     
public partial class StaffInformation : Form
15     {
16         
public StaffInformation()
17         {
18             InitializeComponent();
19         }
20
21         
private void StaffInformation_Load(object sender, EventArgs e)
22         {
23             
// TODO: This line of code loads data into the 'hospitalDataSet.staff' table. You can move, or remove it, as needed.
24             
this.staffTableAdapter.Fill(this.hospitalDataSet.staff);
25             
using (SqlConnection con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True"))
26             {
27
28                 
string str2 = "SELECT * FROM staff";
29                 SqlCommand cmd2 =
new SqlCommand(str2, con1);
30                 SqlDataAdapter da =
new SqlDataAdapter(cmd2);
31                 DataTable dt =
new DataTable();
32                 da.Fill(dt);
33                 dataGridView1.DataSource =
new BindingSource(dt, null);
34             }
35             SqlConnection con =
new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True");
36             con.Open();
37             
string str1 = "select max(id) from staff;";
38
39             SqlCommand cmd1 =
new SqlCommand(str1, con);
40             SqlDataReader dr = cmd1.ExecuteReader();
41             
if (dr.Read())
42             {
43                 
string val = dr[0].ToString();
44                 
if (val == "")
45                 {
46                     textBox1.Text =
"1";
47                 }
48                 
else
49                 {
50                     
int a;
51                     a = Convert.ToInt32(dr[
0].ToString());
52                     a = a +
1;
53                     textBox1.Text = a.ToString();
54                 }
55             }
56             con.Close();
57         }
58
59         
private void button1_Click(object sender, EventArgs e)
60         {
61             SqlConnection con =
new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True");
62             con.Open();
63             
string gen = string.Empty;
64             
if (radioButton1.Checked)
65             {
66                 gen =
"Male";
67             }
68             
else
69             {
70                 gen =
"Female";
71             }
72             
try
73             {
74                 
string str = "INSERT INTO staff(name,gender,position,salary,contact,address) VALUES('" + textBox2.Text + "','" + gen + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "'); ";
75
76                 SqlCommand cmd =
new SqlCommand(str, con);
77                 cmd.ExecuteNonQuery();
78                 
string str1 = "select max(Id) from staff;";
79
80                 SqlCommand cmd1 =
new SqlCommand(str1, con);
81                 SqlDataReader dr = cmd1.ExecuteReader();
82                 
if (dr.Read())
83                 {
84                     MessageBox.Show(
"Staff Information Saved Successfully..");
85                     textBox2.Text =
"";
86                     textBox4.Text =
"";
87                     textBox5.Text =
"";
88                     textBox6.Text =
"";
89                     textBox7.Text =
"";
90                     
using (SqlConnection con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True"))
91                     {
92
93                         
string str2 = "SELECT * FROM staff";
94                         SqlCommand cmd2 =
new SqlCommand(str2, con1);
95                         SqlDataAdapter da =
new SqlDataAdapter(cmd2);
96                         DataTable dt =
new DataTable();
97                         da.Fill(dt);
98                         dataGridView1.DataSource =
new BindingSource(dt, null);
99                     }
100                 }
101             }
102             
catch (SqlException excep)
103             {
104                 MessageBox.Show(excep.Message);
105             }
106             con.Close();
107         }
108
109         
private void button2_Click(object sender, EventArgs e)
110         {
111             SqlConnection con =
new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True");
112
113             con.Open();
114             
if (textBox1.Text != "")
115             {
116                 
try
117                 {
118                     
string getCust = "select name,gender,position,salary,contact,address from staff where id=" + Convert.ToInt32(textBox1.Text) + " ;";
119
120                     SqlCommand cmd =
new SqlCommand(getCust, con);
121                     SqlDataReader dr;
122                     dr = cmd.ExecuteReader();
123                     
if (dr.Read())
124                     {
125                         textBox2.Text = dr.GetValue(
0).ToString();
126                         
if (dr[1].ToString() == "Male")
127                         {
128                             radioButton1.Checked =
true;
129                         }
130                         
else
131                         {
132                             radioButton2.Checked =
true;
133                         }
134                        
135                         textBox4.Text = dr.GetValue(
2).ToString();
136                         textBox5.Text = dr.GetValue(
3).ToString();
137                         textBox6.Text = dr.GetValue(
4).ToString();
138                         textBox7.Text = dr.GetValue(
5).ToString();
139                        
140                     }
141                     
else
142                     {
143                         MessageBox.Show(
" Sorry, This ID, " + textBox1.Text + " Staff is not Available. ");
144                         textBox1.Text =
"";
145                     }
146                 }
147                 
catch (SqlException excep)
148                 {
149                     MessageBox.Show(excep.Message);
150                 }
151                 con.Close();
152             }
153         }
154
155         
private void button4_Click(object sender, EventArgs e)
156         {
157             SqlConnection con =
new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True");
158             con.Open();
159             
string gen = string.Empty;
160             
if (radioButton1.Checked)
161             {
162                 gen =
"Male";
163             }
164             
else
165             {
166                 gen =
"Female";
167             }
168             
try
169             {
170                 
string str = " Update staff set name='" + textBox2.Text + "',gender='" + gen + "',position='" + textBox4.Text + "',salary='" + textBox5.Text + "',contact='" + textBox6.Text + "',address='" + textBox7.Text + "' where id='" + textBox1.Text + "'";
171
172                 SqlCommand cmd =
new SqlCommand(str, con);
173                 cmd.ExecuteNonQuery();
174
175                 
string str1 = "select max(id) from staff;";
176
177                 SqlCommand cmd1 =
new SqlCommand(str1, con);
178                 SqlDataReader dr = cmd1.ExecuteReader();
179                 
if (dr.Read())
180                 {
181                     MessageBox.Show(
"" + textBox2.Text + "'s Details is Updated Successfully.. ", "Important Message");
182                     textBox2.Text =
"";
183                     textBox4.Text =
"";
184                     textBox5.Text =
"";
185                     textBox6.Text =
"";
186                     textBox7.Text =
"";
187                     
using (SqlConnection con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Sem.4\C# Projects\Trials\HospitalManagementSystemCSharp\HospitalManagementSystemCSharp\hospital.mdf;Integrated Security=True"))
188                     {
189
190                         
string str2 = "SELECT * FROM staff";
191                         SqlCommand cmd2 =
new SqlCommand(str2, con1);
192                         SqlDataAdapter da =
new SqlDataAdapter(cmd2);
193                         DataTable dt =
new DataTable();
194                         da.Fill(dt);
195                         dataGridView1.DataSource =
new BindingSource(dt, null);
196                     }
197                 }
198             }
199             
catch (SqlException excep)
200             {
201                 MessageBox.Show(excep.Message);
202             }
203             con.Close();
204         }
205
206         
private void button3_Click(object sender, EventArgs e)
207         {
208             textBox2.Text =
"";
209             textBox4.Text =
"";
210             textBox5.Text =
"";
211             textBox6.Text =
"";
212             textBox7.Text =
"";
213             textBox1.Text =
"";
214         }
215     }
216 }


TODO: This line of code loads data into the 'hospitalDataSet.staff' table. You can move, or remove it, as needed.



Gõ tìm kiếm nhanh...